﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

public class PlayerController : ActorController
{

    #region Private Fields
    private KeyEntryList _keyEntryList;
    // Holds the definitions of the combos the player can pull off
    // TODO: add character-specific combo-defines, if the player is able to play different chars
    private ComboDefineList _combos;
    #endregion

    new protected void Start ()
    {
        // Call parent start
        base.Start();

        // Load predefined combos
        Stream fs;
        XmlSerializer serializer = new XmlSerializer(typeof(ComboDefineList));
        string defFilePath = Path.Combine(Application.dataPath, ComboDefine.DEFINE_FILE);

        fs = new FileStream(defFilePath, FileMode.Open);
        _combos = serializer.Deserialize(fs) as ComboDefineList;
        fs.Close();

        // Load combolist        
        _keyEntryList = new KeyEntryList(ref _combos);
	}

    new protected void Update() 
    {
        // Read input
        InputKey currentKey = InputManager.GetKeyDown();
        _move = new Vector3(Input.GetAxis("Horizontal") * Speed * Time.deltaTime * ActorController.SlowMotionModifier, 0f, 0f);

        // Set facing direction
        if (_move.x > 0)
        {
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }
        else if(_move.x < 0)
        {
            transform.rotation = Quaternion.Euler(0f, 180f, 0f);
        }

        // Check if player wants to jump (and is allowed to)
        if (currentKey == InputKey.Jump && _charController.velocity.y == 0)
        {
            Jump();
        }

        // Add last input to combo list
        if(currentKey != InputKey.None) _keyEntryList.AddNew(currentKey, Time.frameCount);

        // Call parent update
        base.Update();
	}

    // Combotesting
    void OnGUI()
    {
        Rect pos = new Rect(5, 5, 100, Screen.height - 10);
        GUI.Label(pos, _keyEntryList.Print());
    }

}
